const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//id
    manager_name:"",//项目负责人
    manager_num:"",//项目负责人上传号
    gybz:"",//工艺编制
    bzrq:"",//编制日期
    show_bzrq:false,//编制日期
    gybb:"",//工艺版本
    yxj:"",//优先级
    start_time:"",//生产准备开始时间
    show_start_time:false,
    end_time:"",//生产结束时间
    show_end_time:false,
    product_type:"",//产品类型
    material_no:"",//材质
    material_method:"",//下料方式
    mpsl:"",//毛坯数量
    num:"",//加工数量
    setManager:null,
    pic_no:"",
    material_id:"",//原材料id
    material_attr:"",
    mindate:Math.round((new Date() - 365*86400*1000)),
    __returnParams: null,
    //----------------------下方是工序单 单个工序
    processes:[],//工序列表
    showsel:false,
    process:[],//工序名称列表 id: 1, name: "TEST01", desc: "TEST01", content: "TEST01", prepare: "1.5h"
    process_name:[],//只储存↑name
    selNum : 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    let params = {id:options.p_no,}
    request.requestGet('drawing/process_sheet', params, '',function (res) {
      that.setData({
        list:res.data.order,
        gybz:res.data.order.gybz,
        bzrq:res.data.order.bzrq,
        gybb:res.data.order.gybb,
        yxj:res.data.order.yxj,
        start_time:res.data.order.start_time,
        end_time:res.data.order.end_time,
        product_type:res.data.order.product_type,
        material_no:res.data.order.material_no,
        material_method:res.data.order.material_method,
        mpsl:res.data.order.mpsl,
        num:res.data.order.num,
        process:res.data.process,
        pic_no:options.pic_no,
        material_attr:res.data.order.material_attr,
        process:res.data.process,
        material_id:res.data.id,
        processes:res.data.order.processes,
        id:options.p_no

      })
      console.log(res,"ressssssssss")
      console.log(that.data.material_attr,"material_attr")
      for(let i = 0;i<that.data.process.length;i++){
        that.data.process_name.push(that.data.process[i].name)
      }
      that.setData({
        process_name:that.data.process_name
      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onDisplay(event) {
    this.setData({ show_bzrq: true });
  },
  onDisplay1(event) {
    this.setData({ show_start_time: true });
  },
  onDisplay2(event) {
    this.setData({ show_end_time: true });
  },
  onClose() {
    this.setData({ show_bzrq: false });
    this.setData({ show_end_time: false });
    this.setData({ show_start_time: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },

  start(event) {
    const tmptime = this.formatDate(event.detail)
    let valueName = event.target.dataset.variable;
    this.setData({
      [valueName]:tmptime
    });
    this.onClose()
  },
  bindKeyInputVant(event){
    let valueName = event.target.dataset.variable;
    this.setData({
      [valueName]:event.detail
    });
  },

  bindKeyInputVant1(e){
      var arrName = e.currentTarget.dataset.arrName;
      var index = e.currentTarget.dataset.index;
      var variable = e.currentTarget.dataset.variable;
      var arr = this.data.processes;
      arr[index][variable] = e.detail
      this.setData({
          [arrName]:arr
      })
  },
  selmanager(event){
    var index = event.currentTarget.dataset.index
    wx.navigateTo({
      url: `/pages/selmanager/selmanager?index=${index}`,
    })
  },

  onShow() {
    if (this.data.__returnParams) {
      if(this.data.__returnParams[0] === "0"){
        this.data.__returnParams[1] = "暂不选择"
      }
      let userName = this.data.__returnParams[1]
      let userNum = this.data.__returnParams[0]
      this.setData({ 
        manager_name:userName,
        manager_num:userNum,
        __returnParams: null 
      }) // 清空避免重复触发
    }
  },
  delinfo(e){
    let currentList = this.data.processes
      //NOTE 获取传来的index
      var index = e.currentTarget.dataset.index;
      currentList.splice(index, 1)
        this.setData({
          processes: currentList
        })
  },
  upinfo(e){
    if(e.currentTarget.dataset.index === 0){
      return
    }else{
      let tmpNum = e.currentTarget.dataset.index - 1
      let tmp1 = this.data.processes[e.currentTarget.dataset.index]//当前数据
      let tmp2 = this.data.processes[tmpNum]//要移动的数据
      let tmp3 = tmp1.process_id//当前数据id
      let tmp4 = tmp2.process_id//要移动的数据id
      tmp1.process_id = tmp4
      tmp2.process_id = tmp3
      this.setData({
        [`processes.${e.currentTarget.dataset.index}`]: this.data.processes[tmpNum],
        [`processes.${tmpNum}`]: this.data.processes[ e.currentTarget.dataset.index ]
      })
    }
  },
  downinfo(e){
    if(e.currentTarget.dataset.index === this.data.processes.length-1){
      return
    }else{
      let tmpNum = e.currentTarget.dataset.index + 1
      let tmp1 = this.data.processes[e.currentTarget.dataset.index]//当前数据
      let tmp2 = this.data.processes[tmpNum]//要移动的数据
      let tmp3 = tmp1.process_id//当前数据id
      let tmp4 = tmp2.process_id//要移动的数据id
      tmp1.process_id = tmp4
      tmp2.process_id = tmp3
      this.setData({
        [`processes.${e.currentTarget.dataset.index}`]: this.data.processes[tmpNum],
        [`processes.${tmpNum}`]: this.data.processes[ e.currentTarget.dataset.index ]
      })
    }
  },
  selMaterial(e){
    wx.navigateTo({
      url: `/pages/material_management/material_management`,
    })
  },
  submit(e){
    var that = this
    let tmpdata = {
      id:that.data.id,
      order: {
        gybb:that.data.gybb || "",
        material_id:that.data.material_id || "",
        start_time:that.data.start_time || "",
        end_time:that.data.end_time || "",
        bzrq:that.data.bzrq || "",
        yxj:that.data.yxj || "",
        gybz:that.data.gybz || "",
        manager:that.data.manager_num || "",
        fszjth:that.data.product_type || "",
        product_type:that.data.product_type || "",
        material_method:that.data.material_method || "",
        mpsl:that.data.mpsl || "",
        material_id:that.data.material_id || "",
        material_no:that.data.material_no || "",
        material_attr:that.data.material_attr || "",
        processes:that.data.processes || "",
        process_id:that.data.id || "",
        pic_no:that.data.pic_no || "",
        p_no:that.data.p_no || ""
      }
    }
    request.requestAjax('drawing/save_process_sheet',tmpdata,"加载中",function(res){
      if(res.status==200){
          wx.navigateBack()
          return false;
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }
      },function(res){
        wx.showToast({
          title: '请求失败',
          icon:"none"
        })
      });
  },
  addInfo(e){
    let currentList = this.data.processes
    currentList.push({content:"",department:"",desc:"",prepare:"",quota:"",device_id:"",device_name:"",process_id:"",})
    this.setData({
      processes:currentList
    })
  },
  selName(e){
    var that = this
    // let tmpNum = e.detail.value
    this.setData({
      selNum:e.detail.value,
      showsel:!that.data.showsel
    })
    let tmpdata = that.data.process[e.detail.value]
    console.log(tmpdata,"---tmpdata--")
    //设置工序名称和自带数据
    if(tmpdata === void 0){
    }else{
      var index = e.currentTarget.dataset.index;
      that.data.processes[index].name = tmpdata.name
      that.data.processes[index].content = tmpdata.content
      that.data.processes[index].prepare = tmpdata.prepare
      that.data.processes[index].quota = tmpdata.quota
      that.data.processes[index].device_id = tmpdata.device_id
      that.data.processes[index].process_id = Number(this.data.id) 
      that.data.processes[index].device_name = tmpdata.device_name

      
      this.setData({
        processes:that.data.processes
      })
    }
    
  },
  handleCancel() {
    // 无特殊返回内容
    this.setData({
      showsel:false
    })
  },
  onClosePopup(){
    this.setData({
      showsel:false
    })
  },

  showModal() {
    const animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease-out'
    })
    animation.translateY('-100%').step()
    this.setData({ 
      animationData: animation.export(),
      show: true
    })
  },
  setManager(){
    if (this.data.setManager) {
      this.setData({ 
        material_no:this.data.setManager[0],//材质
        material_attr:this.data.setManager[1],
        material_id:this.data.setManager[2],
        setManager: null 
      }) // 清空避免重复触发
    }
  },
})